package com.iotechn.unimall.admin.api.article;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.domain.CmsArticleTypeDO;
import com.iotechn.unimall.data.mapper.CmsArticleTypeMapper;
import com.iotechn.unimall.data.model.Page;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@Service
public class CmsArticleTypeServiceImpl implements CmsArticleTypeService {
    @Autowired
    private CmsArticleTypeMapper cmsArticleTypeMapper;

     @Override
        public Page<CmsArticleTypeDO> list(Integer page, Integer limit, Long adminId) throws ServiceException {
            Wrapper<CmsArticleTypeDO> wrapper = new EntityWrapper<CmsArticleTypeDO>();
            wrapper.orderBy("id", true);
            Integer count = cmsArticleTypeMapper.selectCount(wrapper);
            List<CmsArticleTypeDO> CmsArticleTypeDOS = cmsArticleTypeMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
            return new Page<>(CmsArticleTypeDOS, page, limit, count);
        }

        @Override
        @Transactional(rollbackFor = Exception.class)
        public CmsArticleTypeDO create(CmsArticleTypeDO cmsArticleTypeDO, Long adminId) throws ServiceException {
            cmsArticleTypeDO.setAdminId(adminId);
            Date now=new Date();
            cmsArticleTypeDO.setGmtCreate(now);
            cmsArticleTypeDO.setGmtUpdate(now);
            if (cmsArticleTypeMapper.insert(cmsArticleTypeDO) > 0) {
                cmsArticleTypeDO.setId(cmsArticleTypeDO.getId());
                return cmsArticleTypeDO;
            }
             throw new AdminServiceException(ExceptionDefinition.APP_UNKNOWN_EXCEPTION);
        }

        @Override
        @Transactional(rollbackFor = Exception.class)
        public String update(CmsArticleTypeDO cmsArticleTypeDO, Long adminId) throws ServiceException {
            Long id = cmsArticleTypeDO.getId();
            if (id == null) {
               return "fail";
            }
            Date now=new Date();
            cmsArticleTypeDO.setGmtUpdate(now);
            if (cmsArticleTypeMapper.updateById(cmsArticleTypeDO) > 0) {
                return "ok";
            }
            throw new AdminServiceException(ExceptionDefinition.APP_UNKNOWN_EXCEPTION);
     }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delete(Long id, Long adminId) throws ServiceException {
        if (cmsArticleTypeMapper.deleteById(id) > 0) {
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.APP_UNKNOWN_EXCEPTION);

     }

    @Override
    public CmsArticleTypeDO detail(Long id, Long adminId) throws ServiceException {
       return cmsArticleTypeMapper.selectById(id);
    }

    @Override
        public Boolean updateStatus(Long adminId, Long id,Integer status) throws ServiceException {
            if(status == null || (status != 0 && status != 1)){
                throw new AdminServiceException(ExceptionDefinition.USER_INFORMATION_MISSING);
            }
            CmsArticleTypeDO cmsArticleTypeDO = new CmsArticleTypeDO();
            cmsArticleTypeDO.setId(id);
            cmsArticleTypeDO.setGmtUpdate(new Date());
            if(cmsArticleTypeMapper.updateById(cmsArticleTypeDO) > 0){
                return true;
            }
            return false;
        }

}